<?php
class addMatchResult extends ActionHandler 
{
	function handleRequest($jsonData)
	{
		$userId = $jsonData['userId'];
		$params = $jsonData['params'];
		$builderName = $params['builderName'];
		$matchIndex = $params['matchIndex'];
		$population = $params['population'];
		$height = $params['height'];
		$return = array();
		for ($i = 0; $i < 4; $i++)
		{
			$sqltemp = array("dayofyear(buildDate) = dayofyear(CURRENT_TIMESTAMP) and type = 0", "weekofyear(buildDate) = weekofyear(CURRENT_TIMESTAMP) and type = 1", "month(buildDate) = month(CURRENT_TIMESTAMP) and type = 2", "type = 3");
			$sql = "select count(*) as ranking from ".$this->tables['match_profile']." where matchIndex = ".$matchIndex." and ".$sqltemp[$i]." and population >= ".$population;
			$result = $this->db->query($sql);
			$ranking = 0;
			if ($arr = $this->db->fetchArray($result))
			{
				$ranking = $arr["ranking"];
			}
			if ($ranking < 50)
			{
				$sql = "insert into ".$this->tables['match_profile']." (matchIndex, height, population, builderId, builderName, type) values (".$matchIndex.", ".$height.", ".$population.", ".$userId.", '".$builderName."', ".$i.")";
				$result = $this->db->query($sql);
				$return[$i] = $ranking;
			}
			else
			{
				$return[$i] = -1;
			}
		}
		return $return;
	}
}
?>
